﻿using System;
using System.Collections.Generic;
using System.Linq;
using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using FangChanCrmModel;
using Maticsoft.Model;
using Microsoft.EntityFrameworkCore;

namespace FangChanCrm.Services.Realization
{
    public class t_XiaoShouBiaoService : It_XiaoShouBiaoService
    {
        public readonly MyContext _Context;
        public t_XiaoShouBiaoService(MyContext myContext)
        {
            _Context = myContext;
        }

        public t_XiaoShouBiao Add(t_XiaoShouBiao t_XiaoShouBiao)
        {
            _Context.t_XiaoShouBiao.Add(t_XiaoShouBiao);
            _Context.SaveChanges();
            return _Context.t_XiaoShouBiao.FirstOrDefault(x => x.BeiZhu1 == t_XiaoShouBiao.BeiZhu1 && x.BeiZhu2 == t_XiaoShouBiao.BeiZhu2 && x.BeiZhu3 == t_XiaoShouBiao.BeiZhu3 && x.BeiZhu4 == t_XiaoShouBiao.BeiZhu4 && x.BeiZhu5 == t_XiaoShouBiao.BeiZhu5 && x.BeiZhu6 == t_XiaoShouBiao.BeiZhu6 && x.BeiZhu7 == t_XiaoShouBiao.BeiZhu7 && x.BeiZhu8 == t_XiaoShouBiao.BeiZhu8 && x.BeiZhu9 == t_XiaoShouBiao.BeiZhu9 && x.BeiZhu10 == t_XiaoShouBiao.BeiZhu10 && x.KeHuId == t_XiaoShouBiao.KeHuId && x.ShengChengShiJian == t_XiaoShouBiao.ShengChengShiJian && x.YeWuYuanId == t_XiaoShouBiao.YeWuYuanId && x.ZhiYeGuWenId == t_XiaoShouBiao.ZhiYeGuWenId && x.ZhuangTai == t_XiaoShouBiao.ZhuangTai && x.FangWuId == t_XiaoShouBiao.FangWuId && x.GouMaiFangShi == t_XiaoShouBiao.GouMaiFangShi && x.ChengJiaoJinE == t_XiaoShouBiao.ChengJiaoJinE && x.QueRenRen == t_XiaoShouBiao.QueRenRen);
        }

        public t_XiaoShouBiao GetById(int id)
        {
            return _Context.t_XiaoShouBiao.FirstOrDefault(x => x.Id == id);
        }

        public List<t_XiaoShouBiao> GetList()
        {
            return _Context.t_XiaoShouBiao.Include(x => x.t_KeHuBiao).Include(x => x.t_XiaoShouXiangXiBiaos).ToList();
        }


        public bool Delete(int id)
        {
            _Context.t_XiaoShouBiao.Remove(_Context.t_XiaoShouBiao.Where(x => x.Id == id).ToList()[0]);
            return _Context.SaveChanges() > 0;
        }

        public List<t_XiaoShouBiao> GetAllBaoBeiAndDaiFangByPersonId(int id)
        {
            var list = _Context.t_XiaoShouBiao.Include(x => x.t_KeHuBiao).Include(x => x.t_RenYuanBiao_YeWuYuan).Include(x => x.t_XiaoShouXiangXiBiaos).ToList();
            for (int i = list.Count - 1; i >= 0; i--)
            {
                list[i].t_XiaoShouXiangXiBiaos = list[i].t_XiaoShouXiangXiBiaos.Where(x => x.XiaoShouDongZuo == "报备" || x.XiaoShouDongZuo == "带访").ToList();
                if (list[i].t_XiaoShouXiangXiBiaos.Count == 0)
                {
                    list.Remove(list[i]);
                }
                else
                {
                    if (list[i].YeWuYuanId != id)
                    {
                        list[i].t_KeHuBiao.ShouJiHaoMa = list[i].t_KeHuBiao.ShouJiHaoMa.Substring(0, 3) + "****" + list[i].t_KeHuBiao.ShouJiHaoMa.Substring(7, 4);
                    }
                }
            }
            return list;
        }

        public t_FangYuanKeHuGuanLianBiao fykhAdd(t_FangYuanKeHuGuanLianBiao t_FangYuanKeHuGuanLianBiao)
        {
            _Context.t_FangYuanKeHuGuanLianBiao.Add(t_FangYuanKeHuGuanLianBiao);
            _Context.SaveChanges();
            return _Context.t_FangYuanKeHuGuanLianBiao.FirstOrDefault(x => x.BeiZhu1 == t_FangYuanKeHuGuanLianBiao.BeiZhu1 && x.BeiZhu2 == t_FangYuanKeHuGuanLianBiao.BeiZhu2 && x.BeiZhu3 == t_FangYuanKeHuGuanLianBiao.BeiZhu3 && x.BeiZhu4 == t_FangYuanKeHuGuanLianBiao.BeiZhu4 && x.BeiZhu5 == t_FangYuanKeHuGuanLianBiao.BeiZhu5 && x.BeiZhu6 == t_FangYuanKeHuGuanLianBiao.BeiZhu6 && x.BeiZhu7 == t_FangYuanKeHuGuanLianBiao.BeiZhu7 && x.BeiZhu8 == t_FangYuanKeHuGuanLianBiao.BeiZhu8 && x.BeiZhu9 == t_FangYuanKeHuGuanLianBiao.BeiZhu9 && x.BeiZhu10 == t_FangYuanKeHuGuanLianBiao.BeiZhu10 && x.KeHuId == t_FangYuanKeHuGuanLianBiao.KeHuId && x.FangYuanId == t_FangYuanKeHuGuanLianBiao.FangYuanId && x.XiaoShouBiaoId == t_FangYuanKeHuGuanLianBiao.XiaoShouBiaoId && x.ChengJiaoJinE == t_FangYuanKeHuGuanLianBiao.ChengJiaoJinE);
        }
    }
}
